#coding:utf-8
import json;from jsonpath_rw import parse
from Z_other.other.get_data import GetData
import operator


class CommonUtil:

	def __init__(self):
		self.data = GetData()

	def get_result_value(self,result_json,key):
		try:
			male = parse(key).find(result_json)
			result_value = [match.value for match in male][0]
		except:
			# print (json.dumps(response_data, ensure_ascii=False))
			print('错误：提取响应value失败！')
		else:
			# print (a01)
			return result_value

	def is_equal_value(self,expect,result_json,key):
		# 判断是否相等
		result_value = self.get_result_value(result_json,key)
		value_eq = operator.eq(expect, result_value)
		if value_eq==True:
			a01 = True
		else:
			a01 = False
		return a01

	def is_equal_str(self,one,two):
		if one in two:
			return True
		elif '无效的用户名或密码' in two:
			print('无效的用户名或密码')
		else:
			print(two)
			return False

	def is_equal_dict(self,dict_one,dict_two):
		'''
		判断两个字典是否相等;
		dict_one  存在数据库的响应结果
		dict_two  返回的响应
		'''
		if isinstance(dict_one,str):
			dict_one = json.loads(dict_one)
		if isinstance(dict_two,str):
			dict_two = json.loads(dict_two)
		return operator.eq(dict_one,dict_two)

	def assert_res(self, i, expect, result_key, res):
		pass_count = 0
		fail_count = 0
		error_count = 0
		if res == None:
			res_s = None
		else:
			try:
				res_s = res.json()
			except:
				res_s = res.text
		if type(res_s) == dict:
			if self.is_equal_value(expect, res_s, result_key) == True:
				self.data.write_result(i, 'pass')
				pass_count=1
			else:
				self.data.write_result(i, res.text)
				fail_count=1
		elif type(res_s) == str:
			if self.is_equal_str(expect, res_s) == True:
				self.data.write_result(i, 'pass')
				pass_count=1
			else:
				self.data.write_result(i, 'fail')
				fail_count=1
		else:
			self.data.write_result(i, '！服务器拒绝连接（服务器未启动）')
			error_count=1
		return pass_count, fail_count, error_count

	def assert__(self, i, expect, result_key, res):
		if res == None:
			res_s = None
		else:
			try:
				res_s = res.json()
			except:
				res_s = res.text
		if type(res_s) == dict:
			if self.is_equal_value(expect, res_s, result_key) == True:
				# self.data.write_result(i, 'pass')
				res__ = True
			else:
				# self.data.write_result(i, res.text)
				res__ = False
		elif type(res_s) == str:
			if self.is_equal_str(expect, res_s) == True:
				# self.data.write_result(i, 'pass')
				res__ = True
			else:
				# self.data.write_result(i, 'fail')
				res__ = False
		else:
			# self.data.write_result(i, '！服务器拒绝连接（服务器未启动）')
			res__ = None
		return res__



	# def assert_add(self):
	#
	# 	print('pass：%s' % pass_count)
	# 	print('fail: %s' % fail_count)
	# 	print('error: %s' % error_count)


# def is_contain(self,str_one,str_two):
# 	'''
# 	判断一个字符串是否再另外一个字符串中
# 	str_one:查找的字符串
# 	str_two：被查找的字符串
# 	'''
# 	flag = None
# 	if isinstance(str_one,unicode):
# 		str_one = str_one.encode('unicode-escape').decode('string_escape')
# 		return cmp(str_one,str_two)
#
# 	if str_one in str_two:
# 		flag = True
# 	else:
# 		flag = False
# 	return flag